<?php
namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Validate;
use think\View;
// use plugins\ali_sms;

class VerificationCodeController extends RestBaseController
{
    public function send()
    {
        $sid = input('sid', false);
        if (!$sid) {
          $this->error('图片验证码错误');
        }

        session_id($sid);

        $validate = new Validate([
            'tel_num' => 'require',
        ]);

        $validate->message([
            'tel_num.require' => '请输入手机号！',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        if (!cmf_check_mobile($data['tel_num'])) {
            $this->error("请输入正确的手机格式！");
        }

        // if(!captcha_check($data['captcha'])){
        //   $this->error('图片验证码错误!');
        // };

        //TODO 限制 每个ip 的发送次数

        $code = cmf_get_verification_code($data['tel_num']);
        if (empty($code)) {
            $this->error("验证码发送过多,请明天再试！");
        }

        // $sms = new ali_sms();
        // $result = $sms->sendMobileVerificationCode([
        //   'mobile' => $data['tel_num'],
        //   'mobile' => $code,
        // ]);
        //
        // return $result;

        // 发送验证码
        $param  = ['mobile' => $data['tel_num'], 'code' => $code];
        $result = hook_one("send_mobile_verification_code", $param);

        if ($result !== false && !empty($result['error'])) {
            $this->error($result['message']);
        }

        if ($result === false) {
            $this->error('未安装验证码发送插件,请联系管理员！');
        }

        cmf_verification_code_log($data['tel_num'], $code);

        if (!empty($result['message'])) {
            $this->success($result['message']);
        } else {
            $this->success('验证码已经发送成功！');
        }


    }

}
